package top.linuyx.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.linuyx.exception.GlobalException;
import top.linuyx.pojo.param.ShippingAddressInsertParam;
import top.linuyx.pojo.param.ShippingAddressUpdateParam;
import top.linuyx.pojo.response.ResponseData;
import top.linuyx.pojo.vo.ShippingAddressVO;
import top.linuyx.service.ShippingAddressService;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author Linuyx
 * @Description ShippingAddressController
 * @Date Created in 2021-04-20 14:21
 */

@Controller
@RequestMapping("/shipping-address")
public class ShippingAddressController {

    @Autowired
    private ShippingAddressService shippingAddressService;

    @GetMapping(name = "根据用户名查询所有收货地址")
    @RequiresRoles("user")
    @ResponseBody
    public ResponseData<List<ShippingAddressVO>> listAllShippingAddressByUsername() {
        return ResponseData.success(
                "查询成功",
                shippingAddressService.listAllShippingAddressByUsername()
        );
    }

    @PostMapping(name = "添加收货地址")
    @RequiresRoles("user")
    @ResponseBody
    public ResponseData<String> insertAddress(@Valid @RequestBody ShippingAddressInsertParam insertParam) {
        shippingAddressService.insertAddress(insertParam);
        return ResponseData.success("添加成功");
    }

    @PutMapping(name = "根据id修改收货地址")
    @RequiresRoles("user")
    @ResponseBody
    public ResponseData<String> updateAddressById(@Valid @RequestBody ShippingAddressUpdateParam updateParam) {
        shippingAddressService.updateAddressById(updateParam);
        return ResponseData.success("修改成功");
    }

    @DeleteMapping(value = "/{id}", name = "根据id删除收货地址")
    @RequiresRoles("user")
    @ResponseBody
    public ResponseData<String> deleteAddressById(@PathVariable("id") Long shippingAddressId){
        if(shippingAddressId == null){
            throw new GlobalException("收货地址id不允许为空");
        }
        shippingAddressService.deleteAddressById(shippingAddressId);
        return ResponseData.success("删除成功");
    }

}
